#include "cmddefend.h"

#include "../mainctrl.h"
#include "../cmd/dataprepare.h"
#include "commmsg.h"


CT_REGISTER_ASYNCCMD(ID_MAIN_GET_DEFEND_INFO, CCmdGetDefendInfo);

int CCmdGetDefendInfo::Do(void* pData)
{
    CT_ASSERT(pData);

    //解析消息
    CCmdPBDoParam* pstParam = (CCmdPBDoParam*) pData;
    pstMainCtrl = (CMainCtrl*) pstParam->m_pMain;

    if (pstParam->m_pstMsgHead->msgid() != ID_MAIN_GET_DEFEND_INFO||
        pstParam->m_pstMsgHead->msgtype() != CSMsgDef::EMT_REQUEST)
    {
        CT_ERROR(("Cmd Stat Err Cmd=%d CmdID=%d Tpye=%d",
            ID_MAIN_GET_DEFEND_INFO,
            pstParam->m_pstMsgHead->msgid(),
            pstParam->m_pstMsgHead->msgtype()));
        return Done(RET_SYS_ERROR);
    }

    stMsgHead = *(pstParam->m_pstMsgHead);
    stEnpHead = *(pstParam->m_pstEnpHead);
    int iRet = pstMainCtrl->DecodePBMsg(stReq, pstParam);
    if (iRet)
    {
        return AnsClients(iRet);
    }
    //消息解析结束

    uiGID = stReq.gid();
    CT_TRACE(("get Defend GID: %u", uiGID));

    //检查登录
    iRet = pstMainCtrl->CheckLogin(uiGID, stEnpHead);
    if (iRet != RET_OK)
    {
        return AnsClients(iRet);
    }

    LogicDo(RET_OK);
    return RET_OK;
}

int CCmdGetDefendInfo::LogicDo(int iRet)
{
    if (iRet != 0)
    {
        return AnsClients(iRet);
    }

    PREPARE_PLAYER_ONLINE_DATA(this, uiGID);

    int iIndex = pstMainCtrl->m_stPlayerOnlineDataHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        return AnsClients(RET_SYS_ERROR);
    }
    CPlayerOnlineData& stPlayerOnlineData = pstMainCtrl->m_stPlayerOnlineDataHash[iIndex];
    
    stAns.set_gid(uiGID);
    stAns.set_valid_status(stPlayerOnlineData._iDefendValidInfo);
    stAns.set_time_in_day(stPlayerOnlineData._iDefTotalOnlineTime + pstMainCtrl->m_iNow - stPlayerOnlineData._iLoginTime);
    stAns.set_last_login_time(stPlayerOnlineData._iLoginTime);

    /*
    PREPARE_PLAYER_BASE_INFO(this, uiGID);
    ASSERT_PLAYER_PREPARED(uiGID);

    int iGidIndex = pstMainCtrl->m_stPlayerSnsDetailInfoHash.HashFind(uiGID);
    if (iGidIndex < 0)
    {
        CT_ERROR (("Get defend valid info(id = %u), not exist.", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
    }
    CPlayerSnsDetailInfo & stPlayerSnsDetailInfo = pstMainCtrl->m_stPlayerSnsDetailInfoHash[iGidIndex];
    CPlayerSnsCommInfo & stSnsOnlineInfo = stPlayerSnsDetailInfo._stCommInfo;

    stAns.set_gid(uiGID);
    stAns.set_valid_status( stSnsOnlineInfo._iDefendValidInfo );

    if( stSnsOnlineInfo._iDefendValidInfo!=1 )
    {
        struct tm* tm_login = gmtime( (const time_t*)&pstMainCtrl->m_iNow );
        if( !(stSnsOnlineInfo._iCurrentMonth==tm_login->tm_mon
            && stSnsOnlineInfo._iCurrentDay==tm_login->tm_mday) )
        {
            stSnsOnlineInfo._iCurrentMonth = tm_login->tm_mon;
            stSnsOnlineInfo._iCurrentDay = tm_login->tm_mday;
            stSnsOnlineInfo._iTotalOnlineTime = tm_login->tm_hour*3600+tm_login->tm_min*60+tm_login->tm_sec;
        }
        else
        {
            //stSnsOnlineInfo._iTotalOnlineTime += (pstMainCtrl->m_iNow - stSnsOnlineInfo._iLastLogInTime);
            stSnsOnlineInfo._iTotalOnlineTime += (pstMainCtrl->m_iNow - stSnsOnlineInfo._iLastRecordTime);
            stSnsOnlineInfo._iLastRecordTime = pstMainCtrl->m_iNow;
        }

        if( stSnsOnlineInfo._iWarnTimes==4 || stSnsOnlineInfo._iTotalOnlineTime>=10800 )//offline time must in warn times=4
        {
            //stSnsOnlineInfo._iTotalOfflineTime += pstMainCtrl->m_iNow - stSnsOnlineInfo._iLastLogOutTime;
            if( stSnsOnlineInfo._iTotalOnlineTime>=10800 )//如果在线时间超过3小时，多余的时间算进累计离线时间
            {
                int iSegs = stSnsOnlineInfo._iTotalOnlineTime - 10800;
                stSnsOnlineInfo._iTotalOnlineTime = 10800;
                stSnsOnlineInfo._iTotalOfflineTime += iSegs;

                //set offline
                CEnpNetHead stNetHandle;
                pstMainCtrl->m_stPlayerOnline.GetOnlinePlayerHandle(uiGID, stNetHandle ); 
                pstMainCtrl->SetOffline(uiGID, stNetHandle);
            }

            if( stSnsOnlineInfo._iTotalOfflineTime>=18000 )//5*60*60
            {
                stSnsOnlineInfo._iTotalOnlineTime = 0;
                stSnsOnlineInfo._iTotalOfflineTime = 0;
                stSnsOnlineInfo._iWarnTimes = 0;

                //保存数据
                CVarObjectKey stKey;
                stKey.m_chDataType = DT_PLAYER_SNS_DETAIL_INFO;
                stKey.m_u64ID = stPlayerSnsDetailInfo._uiGID;
                pstMainCtrl->UpdateData(stKey, stPlayerSnsDetailInfo, VOP_UPDATE);
            }
        }

        stAns.set_time_in_day( stSnsOnlineInfo._iTotalOnlineTime );
        stAns.set_warn_times( stSnsOnlineInfo._iWarnTimes );
    }
    */
    
    return AnsClients(RET_OK);
}

int CCmdGetDefendInfo::AnsClients(int iRet)
{
    pstMainCtrl->m_stEncodeBuf.Clear();

    stMsgHead.set_msgtype(CSMsgDef::EMT_ANSWER);
    stMsgHead.set_msgret( iRet );

    if (iRet != RET_OK)
    {
        pstMainCtrl->ReturnMsgHead(stMsgHead, stEnpHead);
        return Done(iRet);
    }

    pstMainCtrl->EncodePBMsg(stMsgHead, pstMainCtrl->m_stEncodeBuf);
    pstMainCtrl->EncodePBMsg(stAns, pstMainCtrl->m_stEncodeBuf);

    if (pstMainCtrl->AnswerToClient(stEnpHead, pstMainCtrl->m_stEncodeBuf, true))
    {
        CT_WARNING(("Defend enp package error"));
        return Done(RET_SYS_ERROR);
    }

    return Done(iRet);
}

//////////////////////////////////////////////////////////////////////////
CT_REGISTER_ASYNCCMD(ID_MAIN_COMMIT_DEFEND_INFO, CCmdCommitDefendInfo);

int CCmdCommitDefendInfo::Do(void* pData)
{
    CT_ASSERT(pData);

    //解析消息
    CCmdPBDoParam* pstParam = (CCmdPBDoParam*) pData;
    pstMainCtrl = (CMainCtrl*) pstParam->m_pMain;

    if (pstParam->m_pstMsgHead->msgid() != ID_MAIN_COMMIT_DEFEND_INFO ||
        pstParam->m_pstMsgHead->msgtype() != CSMsgDef::EMT_REQUEST)
    {
        CT_ERROR(("Cmd Stat Err Cmd=%d CmdID=%d Tpye=%d",
            ID_MAIN_COMMIT_DEFEND_INFO,
            pstParam->m_pstMsgHead->msgid(),
            pstParam->m_pstMsgHead->msgtype()));
        return Done(RET_SYS_ERROR);
    }

    stMsgHead = *(pstParam->m_pstMsgHead);
    stEnpHead = *(pstParam->m_pstEnpHead);
    int iRet = pstMainCtrl->DecodePBMsg(stReq, pstParam);
    if (iRet)
    {
        return AnsClients(iRet);
    }
    //消息解析结束

    uiGID = stReq.gid();
    CT_TRACE(("commit defend info:[%u]", uiGID));

    //检查登录
    iRet = pstMainCtrl->CheckLogin(uiGID, stEnpHead);
    if (iRet != RET_OK)
    {
        return AnsClients(iRet);
    }

    LogicDo(RET_OK);
    return RET_OK;
}

int CCmdCommitDefendInfo::LogicDo(int iRet)
{
    if (iRet != 0)
    {
        return AnsClients(iRet);
    }

    PREPARE_PLAYER_BASE_INFO(this, uiGID);
    PREPARE_PLAYER_ONLINE_DATA(this, uiGID);

    int iIndex = pstMainCtrl->m_stPlayerBaseInfoHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        return AnsClients(RET_SYS_ERROR);
    }
    CPlayerBaseInfo& stPlayerBaseInfo = pstMainCtrl->m_stPlayerBaseInfoHash[iIndex];

    iIndex = pstMainCtrl->m_stPlayerOnlineDataHash.HashFind(uiGID);
    if (iIndex < 0)
    {
        return AnsClients(RET_SYS_ERROR); 
    }
    CPlayerOnlineData& stPlayerOnlineData = pstMainCtrl->m_stPlayerOnlineDataHash[iIndex];

    stPlayerBaseInfo._iDefendValidInfo = stReq.valid_status();
    stPlayerOnlineData._iDefendValidInfo = stPlayerBaseInfo._iDefendValidInfo;

    //保存数据
    CVarObjectKey stKey;
    stKey.m_chDataType = DT_PLAYER_BASE_INFO;
    stKey.m_u64ID = uiGID;
    pstMainCtrl->UpdateData(stKey, stPlayerBaseInfo, VOP_UPDATE);

    return AnsClients(RET_OK);
}

int CCmdCommitDefendInfo::AnsClients(int iRet)
{
    stMsgHead.set_msgret(iRet);
    pstMainCtrl->ReturnMsgHead(stMsgHead, stEnpHead);
    return Done(iRet);
}

/////////////////////////////////////////////////////////////////////
CT_REGISTER_ASYNCCMD(ID_MAIN_COMMIT_DEFEND_OTHER, CCmdCommitDefendOtherInfo);

int CCmdCommitDefendOtherInfo::Do(void* pData)
{
    CT_ASSERT(pData);

    //解析消息
    CCmdPBDoParam* pstParam = (CCmdPBDoParam*) pData;
    pstMainCtrl = (CMainCtrl*) pstParam->m_pMain;

    if (pstParam->m_pstMsgHead->msgid() != ID_MAIN_COMMIT_DEFEND_OTHER ||
        pstParam->m_pstMsgHead->msgtype() != CSMsgDef::EMT_REQUEST)
    {
        CT_ERROR(("Cmd Stat Err Cmd=%d CmdID=%d Tpye=%d",
            ID_MAIN_COMMIT_DEFEND_OTHER,
            pstParam->m_pstMsgHead->msgid(),
            pstParam->m_pstMsgHead->msgtype()));
        return Done(RET_SYS_ERROR);
    }

    stMsgHead = *(pstParam->m_pstMsgHead);
    stEnpHead = *(pstParam->m_pstEnpHead);
    int iRet = pstMainCtrl->DecodePBMsg(stReq, pstParam);
    if (iRet)
    {
        return AnsClients(iRet);
    }
    //消息解析结束

    uiGID = stReq.gid();
    CT_TRACE(("commit defend info:[%u]", uiGID));

    //检查登录
    iRet = pstMainCtrl->CheckLogin(uiGID, stEnpHead);
    if (iRet != RET_OK)
    {
        return AnsClients(iRet);
    }

    LogicDo(RET_OK);
    return RET_OK;
}

int CCmdCommitDefendOtherInfo::LogicDo(int iRet)
{
    if (iRet != 0)
    {
        return AnsClients(iRet);
    }

    /*
    PREPARE_PLAYER_BASE_INFO(this, uiGID);
    ASSERT_PLAYER_PREPARED(uiGID);

    int iGidIndex = pstMainCtrl->m_stPlayerSnsDetailInfoHash.HashFind(uiGID);
    if (iGidIndex < 0)
    {
        CT_ERROR (("Get defend commit other valid info(id = %u), not exist.", uiGID));
        return AnsClients(RET_SYSTEM_FAIL);
    }
    CPlayerSnsDetailInfo & stPlayerSnsDetailInfo = pstMainCtrl->m_stPlayerSnsDetailInfoHash[iGidIndex];
    CPlayerSnsCommInfo & stSnsOnlineInfo = stPlayerSnsDetailInfo._stCommInfo;

    if( !(stReq.warn_times()<1 || stReq.warn_times()>4) )
    {
        int iTmpWarnTimes = 0;
        stSnsOnlineInfo._iTotalOnlineTime += (pstMainCtrl->m_iNow - stSnsOnlineInfo._iLastRecordTime);
        stSnsOnlineInfo._iLastRecordTime = pstMainCtrl->m_iNow;

        if( stSnsOnlineInfo._iTotalOnlineTime<3600 )
        {
            iTmpWarnTimes = 0;
        }
        else if( stSnsOnlineInfo._iTotalOnlineTime>=3600 && stSnsOnlineInfo._iTotalOnlineTime<7200 )
        {
            iTmpWarnTimes = 1;
        }
        else if( stSnsOnlineInfo._iTotalOnlineTime>=7200 && stSnsOnlineInfo._iTotalOnlineTime<10500 )
        {
            iTmpWarnTimes = 2;
        }
        else if( stSnsOnlineInfo._iTotalOnlineTime>=10500 && stSnsOnlineInfo._iTotalOnlineTime<10800 )
        {
            iTmpWarnTimes = 3;
        }
        else if( stSnsOnlineInfo._iTotalOnlineTime>=10800 )
        {
            iTmpWarnTimes = 4;
        }
        stSnsOnlineInfo._iWarnTimes = iTmpWarnTimes==stReq.warn_times() ? stReq.warn_times():iTmpWarnTimes;

        //保存数据
        CVarObjectKey stKey;
        stKey.m_chDataType = DT_PLAYER_SNS_DETAIL_INFO;
        stKey.m_u64ID = stPlayerSnsDetailInfo._uiGID;
        pstMainCtrl->UpdateData(stKey, stPlayerSnsDetailInfo, VOP_UPDATE);
        
        if( stSnsOnlineInfo._iWarnTimes==4 )
        {
            //踢掉用户
            if (pstMainCtrl->m_stPlayerOnline.CheckOnline(uiGID))
            {
                CEnpNetHead stNetHandle;
                pstMainCtrl->m_stPlayerOnline.GetOnlinePlayerHandle(uiGID, stNetHandle );
                //设置离线
                pstMainCtrl->SetOffline(uiGID, stNetHandle);
            }
        }
    }
    */

    return AnsClients(RET_OK);
}

int CCmdCommitDefendOtherInfo::AnsClients(int iRet)
{
    stMsgHead.set_msgret(iRet);
    pstMainCtrl->ReturnMsgHead(stMsgHead, stEnpHead);
    return Done(iRet);
}
